define(function(require, exports, module) {
	"require:nomunge,exports:nomunge,module:nomunge";

	// 异步请求
	var wind_async = require('ddf_wind_async');

	var base = require('component-base');
	var history = require('component-history');

	require('component-page');

	var beginDate = "", endDate = "", title = "", content = "", pageSize = 10, pageNumber = 1, oper = "sortQuery";

	/**
	 * 推送消息列表
	 */
	// function loadOperatorList() {
	exports.init = function(m) {
		history.addHis(this, arguments);
		window.$ddf_pushMessage = m;
		resetSearchParams();
		oper = "sortQuery";
		var htmlStr = "";

		htmlStr += '<div id="content-header"><div id="breadcrumb"> <a href=javascript:load("'
				+ ddf.index.index_js
				+ '") title="返回首页" class="tip-bottom"><i class="icon-home"></i> 首页</a><a href="javascript:void(0)" class="current">推送消息查询</a></div></div>';
		htmlStr += '<div class="container-fluid">';
		htmlStr += '<div class="row-fluid">';
		htmlStr += '<div class="span12">';

		htmlStr += '<div id="pushMessageDiv">';
		htmlStr += '<div class="widget-box">';
		htmlStr += '<div class="widget-title"> <span class="icon"><i class="icon-th"></i></span>';
		htmlStr += '<h5>推送消息列表</h5>';
		htmlStr += '</div>';
		htmlStr += '<div class="widget-content nopadding">';
		htmlStr += '<form class="form-horizontal">';
		htmlStr += '<div class="control-group" id="control-group-title">';
		htmlStr += '<label class="control-label">消息标题：</label>';
		htmlStr += '<div class="controls">';
		htmlStr += '<input class="span9" id="input-title" type="text" >';
		htmlStr += '<span class="help-block">消息标题不允许超过30字</span>';
		htmlStr += '</div>';
		htmlStr += '</div>';
		htmlStr += '<div class="control-group" id="control-group-content">';
		htmlStr += '<label class="control-label">消息内容：</label>';
		htmlStr += '<div class="controls">';
		htmlStr += '<textarea id="input-content" class="span9" type="text" rows="5"></textarea>';
		htmlStr += '<span class="help-block">“消息内容”和“消息签名”总长不允许超过200字</span>';
		htmlStr += '</div>';
		htmlStr += '</div>';
		htmlStr += '<div class="control-group" id="control-group-sign">';
		htmlStr += '<label class="control-label">消息签名：</label>';
		htmlStr += '<div class="controls">';
		htmlStr += '<input class="span9" id="input-sign" type="text" disabled="disabled" value="">';
		// htmlStr += '<span class="help-block">包含在消息内容中，请用简称；例：公司便利店</span>';
		htmlStr += '</div>';
		htmlStr += '</div>';
		htmlStr += '<div class="control-group">';
		htmlStr += '<label class="control-label">接收人：</label>';
		htmlStr += '<div class="controls">';
		htmlStr += '<span class="inp_span" id="memberSize"></span>';
		htmlStr += '</div>';
		htmlStr += '</div>';
		htmlStr += '<div class="form-actions">';
		htmlStr += '<button class="btn btn-primary" type="button" onclick=$ddf_pushMessage.pushMessage() >发送</button>&nbsp;&nbsp;';
		htmlStr += '&nbsp;<button class="btn btn-primary" type="button" onclick=$ddf_pushMessage.resetMessage() >重置</button>&nbsp;&nbsp;';
		htmlStr += '</div>';
		htmlStr += '</form>';
		htmlStr += '</div>';
		htmlStr += '</div>';
		htmlStr += '</div>';

		htmlStr += '<div class="widget-box">';
		htmlStr += '<div class="widget-title"><span class="icon"><i class="icon-comment-alt"></i></span>';
		htmlStr += '<h5>已推送消息</h5>';
		htmlStr += '<a onclick=$ddf_pushMessage.havePushMessages() class="label label-success">更多</a>';
		htmlStr += '</div>';
		htmlStr += '<div class="widget-content nopadding collapse in">';
		htmlStr += '<ul id="messageListDiv" class="recent-posts">';

		htmlStr += '</ul>';
		htmlStr += '</div>';
		htmlStr += '</div>';

		htmlStr += '</div>  ';
		htmlStr += '</div>';
		htmlStr += '</div> ';
		htmlStr += '</div> ';

		// $("#content0").css("display", "block");

		showDiv("content0");
//		alert("12");
		$("#content0").html(htmlStr);
		
		$("#input-title").change(function() {

			var title = $("#input-title").val();

			if (isNull(title) || isLength(title, 60)) {
				addErrorMessage("control-group-title", "input-title", "");
			} else {
				clearErrorMessage("control-group-title");
			}

		});

		$("#input-content").change(function() {

			checkMessageLength();

		});

		$("#input-sign").change(function() {

			checkMessageLength();

		});

		getQueryPushMessageList("1", "5", beginDate, endDate, title, content,
				oper);

	};

	function checkMessageLength() {
		var content = $("#input-content").val();
		var sign = $("#input-sign").val();

		if (isNull(content)) {
			addErrorMessage("control-group-content", "input-content",
					"消息内容不能为空");
		} else {
			clearErrorMessage("control-group-content");
		}

		if ((content.length + sign.length) > 400) {
			addErrorMessage("control-group-content", "input-content", "");
			addErrorMessage("control-group-sign", "input-sign", "");
		} else {
			clearErrorMessage("control-group-content");
			clearErrorMessage("control-group-sign");
		}
	}

	/**
	 * 查询推送消息列表
	 */
	function getQueryPushMessageList(pageNumber, pageSize, beginDate, endDate,
			title, content, oper) {
		var params = {
			"page.pageNumber" : pageNumber, // 设置显示记录数
			"page.pageSize" : pageSize, // 设置显示记录数
			"beginDate" : beginDate,
			"endDate" : endDate,
			"title" : title,
			"content" : content
		};

		base.showLoading();

		eval(
				Wind.compile("async", function() {

					var data = $await(wind_async.DDF_AsyncRequest("POST",
							ddf.pushMessage.queryPushMessageList, params));

					getQueryPushMessageListCallBack(data, oper);

				}))().start();
	}

	/**
	 * 查询推送消息列表_回调函数
	 */
	function getQueryPushMessageListCallBack(data, oper) {

		if (data["PUSHMESSAGE"] != true) {
			$("#pushMessageDiv").css("display", "none");
		}

		$("#input-sign").val(data["shopSign"]);
		if (data["code"] == 1) {
			var htmlStr = "";
			if (oper == "sortQuery") {
				for (var i = 0; i < data["pushMessageList"].length; i++) {

					htmlStr += '<li>';
					htmlStr += '<div class="hpMessages"> <span class="user-info"> '
							+ data["pushMessageList"][i]["sign"]
							+ '&nbsp;&nbsp;/&nbsp;&nbsp;时间：'
							+ data["pushMessageList"][i]["createTime"]
							+ '</span>';
					htmlStr += '<div  class="color_f90"><label>【标题】</label><dt>'
							+ data["pushMessageList"][i]["title"]
							+ '</dt></div>';
					htmlStr += '<div><label>【内容】</label><pre>'
							+ data["pushMessageList"][i]["content"]
							+ '</pre></div>';
					htmlStr += '<span class="receiver"> 已阅读：'
							+ data["pushMessageList"][i]["readedNum"]
							+ '位 </span>';
					htmlStr += '</div>';
					htmlStr += '</li>';

				}
				$("#messageListDiv").html(htmlStr);

			} else if (oper == "moreQuery") {
				for (var i = 0; i < data["pushMessageList"].length; i++) {
					htmlStr += '<tr>';
					htmlStr += '<td class="text-center">'
							+ getSequence(data["pageNumber"], i + 1) + '</td>';
					htmlStr += '<td>';
					htmlStr += '<div class="hpMessages"> <span class="user-info"> '
							+ data["pushMessageList"][i]["sign"]
							+ '&nbsp;&nbsp;/&nbsp;&nbsp;时间：'
							+ data["pushMessageList"][i]["createTime"]
							+ '</span>';
					htmlStr += '<div  class="color_f90"><label>【标题】</label><dt>'
							+ data["pushMessageList"][i]["title"]
							+ '</dt></div>';
					htmlStr += '<div><label>【内容】</label><pre>'
							+ data["pushMessageList"][i]["content"]
							+ '</pre></div>';
					htmlStr += '<span class="receiver"> 已阅读：'
							+ data["pushMessageList"][i]["readedNum"]
							+ '位 </span>';
					htmlStr += '</td>';
					htmlStr += '</tr>';

				}
				$("#pushMessageListDiv").html(htmlStr);

				$('#footPageDiv0').ddf_pagination(
						data["pageSize"],
						data["pageNumber"],
						data["totalCount"],
						data["totalPage"],
						{

							changePageCallback : function(pageSize, page) {
								getQueryPushMessageList(page, pageSize,
										beginDate, endDate, title, content,
										oper);

							},
							changePageSizeCallBack : function(pageSize) {
								getQueryPushMessageList(1, pageSize, beginDate,
										endDate, title, content, oper);

							}
						});

			}

		} else {
			if (oper == "sortQuery") {
				$("#messageListDiv").html("");
			} else if (oper == "moreQuery") {
				$("#pushMessageListDiv").html("");
			}
			//base.tips_error(data["message"]);
		}
		$("#memberSize").html("所有会员（" + data["memberSize"] + "人）");
		base.cancelLoading();

	}

	/**
	 * 推送消息
	 */
	exports.pushMessage = function() {
		var title = $("#input-title").val();
		var content = $("#input-content").val();
		var sign = $("#input-sign").val();

		if (isNull(title)) {
			addErrorMessage("control-group-title", "input-title", "消息标题不能为空");
			return;
		}

		if (isNull(content)) {
			addErrorMessage("control-group-content", "input-content",
					"消息内容不能为空");
			return;
		}

		if ((content.length + sign.length) > 400) {
			addErrorMessage("control-group-content", "input-content", "");
			addErrorMessage("control-group-sign", "input-sign", "");
			return;
		}

		var params = {
			"title" : title,
			"content" : content,
			"sign" : sign
		};

		base.showLoading();

		eval(
				Wind.compile("async", function() {

					var data = $await(wind_async.DDF_AsyncRequest("POST",
							ddf.pushMessage.pushMessageToUser, params));

					pushMessageCallBack(data);

				}))().start();
	};

	function pushMessageCallBack(data) {
		if (data["code"] == 1) {

			showDiv("content0");

			base.tips("推送消息成功");
			resetMessage();
			getQueryPushMessageList(pageNumber, pageSize, beginDate, endDate,
					title, content, oper);

		} else {

			base.tips_error(data["message"]);

		}

		base.cancelLoading();
	}

	exports.havePushMessages = function() {
		history.addHis(this, arguments);
		
		var htmlStr = "";
		oper = "moreQuery";

		htmlStr += '<div id="content-header"><div id="breadcrumb"> <a href=javascript:load("'
				+ ddf.index.index_js
				+ '") title="返回首页" class="tip-bottom"><i class="icon-home"></i> 首页</a><a href=javascript:$ddf_pushMessage.showDiv("content0") class="current">推送消息查询</a><a href="javascript:void(0)" class="current">已推送消息</a></div></div>';
		htmlStr += '<div class="container-fluid">';
		htmlStr += '<div class="row-fluid">';
		htmlStr += '<div class="span12">';

		htmlStr += '<div class="widget-box">';
		htmlStr += '<div class="widget-title"> <span class="icon"><i class="icon-th"></i></span>';
		htmlStr += '<h5>操作</h5>';
		htmlStr += '</div>';
		htmlStr += '<div class="widget-content nopadding">';
		htmlStr += '<form class="form-horizontal">';

		htmlStr += '<div class="row-fluid">';
		htmlStr += '<div class="span6"><div class="control-group"><label class="control-label">发送时间：</label><div class="controls"><input class="span4 m-wrap Wdate" type="text" id="beginDate" onClick=WdatePicker({dateFmt:"yyyy-MM-dd",readOnly:false}) />&nbsp;&nbsp;至&nbsp;&nbsp;<input class="span4 m-wrap Wdate" type="text" id="endDate" onClick=WdatePicker({dateFmt:"yyyy-MM-dd",readOnly:false}) /></div></div></div>';

		htmlStr += '<div class="span6">';
		htmlStr += '<div class="control-group">';
		htmlStr += '<label class="control-label">消息标题：</label>';
		htmlStr += '<div class="controls">';
		htmlStr += '<input class="span8 m-wrap" type="text" id="queryTitle"/>';
		htmlStr += '</div>';
		htmlStr += '</div>';

		htmlStr += '</div>';
		htmlStr += '</div>';

		htmlStr += '<div class="row-fluid">';

		htmlStr += '<div class="span6">';
		htmlStr += '<div class="control-group">';
		htmlStr += '<label class="control-label">消息内容：</label>';
		htmlStr += '<div class="controls">';
		htmlStr += '<input class="span8 m-wrap" type="text" id="queryContent"/>';
		htmlStr += '</div>';
		htmlStr += '</div>';
		htmlStr += '</div>';

		htmlStr += '<div class="span6">';
		htmlStr += '</div>';

		htmlStr += '</div>';

		htmlStr += '<div class="form-actions">';
		htmlStr += '<button class="btn btn-primary" type="button" onclick=$ddf_pushMessage.searchPushMessage()>搜索</button>&nbsp;&nbsp;';
		htmlStr += '<button class="btn btn-primary" type="button" onclick=$ddf_pushMessage.resetSearchParams()>重置</button>&nbsp;&nbsp;';
		htmlStr += '<button class="btn btn-info" type="button" onclick=$ddf_pushMessage.showDiv("content0")>返回</button>';
		htmlStr += '</div>';
		htmlStr += '</form>';
		htmlStr += '</div>';
		htmlStr += '</div>';

		htmlStr += '<div class="widget-box">';
		htmlStr += '<div class="widget-title"><span class="icon"><i class="icon-th"></i></span>';
		htmlStr += '<h5>已推送消息</h5>';
		htmlStr += '</div>';
		htmlStr += '<div class="widget-content nopadding dataTables_wrapper">';
		htmlStr += '<table class="table table-bordered table-striped with-check">';
		htmlStr += '<thead>';
		htmlStr += '<tr>';
		htmlStr += '<th>序号</th>';
		htmlStr += '<th>消息</th>';
		htmlStr += '</tr>';
		htmlStr += '</thead>';
		htmlStr += '<tbody id="pushMessageListDiv">';
		htmlStr += '</tbody>';
		htmlStr += '</table>';
		htmlStr += '<div id="footPageDiv0">';

		htmlStr += '</div>';

		htmlStr += '</div>  ';
		htmlStr += '</div>';
		htmlStr += '</div> ';
		htmlStr += '</div> ';
        
		showDiv("content1");

		$("#content1").html(htmlStr);
		
		getQueryPushMessageList(pageNumber, pageSize, beginDate, endDate,
				title, content, oper);
	};

	// 搜索消息
	exports.searchPushMessage = function() {
		var beginDate = $("#beginDate").val();
		var endDate = $("#endDate").val();
		var queryTitle = $("#queryTitle").val();
		var queryContent = $("#queryContent").val();

		if (!base.checkDate(beginDate, endDate)) {
			return;
		}

		getQueryPushMessageList(pageNumber, pageSize, beginDate, endDate,
				queryTitle, queryContent, oper);
	};

	// 重置搜索条件
	function resetSearchParams() {
		beginDate = "";
		endDate = "";
		title = "";
		content = "";

		$("#beginDate").val("");
		$("#endDate").val("");
		$("#queryTitle").val("");
		$("#queryContent").val("");
	}

	function showDiv(block) {

		$("#content > div").hide();

		if ($("#" + block).length <= 0) {

			$("#content").append('<div id="' + block + '"></div>');

		} else {

			$("#" + block).show();
		}

		if (block == "content0") {
			sort = "sortQuery";
			beginDate = "";
			endDate = "";
			title = "";
			content = "";
			pageSize = 10;
			pageNumber = 1;
		}
	}

	/**
	 * 重置（清空）推送消息
	 */
	function resetMessage() {
		$("#input-title").val("");
		$("#input-content").val("");
	}

	// 暴露方法
	exports.showDiv = showDiv;
	exports.resetSearchParams = resetSearchParams;
	exports.resetMessage = resetMessage;

});